<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style>
</head>
<body>
<?php
include_once('../include/functions.php');
$action=fs_get_value("do");
switch($action)
{
	case	"upload_documents"	     : upload_documents()	         ; break;
	default				         : upload()		             ; break;
}
?>
<?php
function upload(){
    $id = md5(uniqid(rand(), true));
?>
<script language="javascript">
var xmlHttp;
var proNum=0;
var loop=0;
 
var Try = {
 these: function() {
 var returnValue;
  for (var i = 0; i < arguments.length; i++) {
   var lambda = arguments[i];
   try {
    returnValue = lambda();
    break;
   } catch (e) {}
  }
  return returnValue;
 }
}
 
function createXHR(){
 return Try.these(
  function() {return new XMLHttpRequest()},
  function() {return new ActiveXObject('Msxml2.XMLHTTP')},
  function() {return new ActiveXObject('Microsoft.XMLHTTP')}
 ) || false;
}
 
var xmlHttp;
 
function sendURL() {
 xmlHttp=createXHR();
 var url="teacher_getprogress.php?progress_key=<?php echo $id;?>";
 xmlHttp.onreadystatechange = doHttpReadyStateChange;
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);  
}
 
function doHttpReadyStateChange() {
 if (xmlHttp.readyState == 4){
  proNum=parseInt(xmlHttp.responseText);
  document.getElementById("progressinner").style.width = proNum+"%";
  document.getElementById("showNum").innerHTML = "已上传"+proNum+"%";
  if ( proNum < 100){
   setTimeout("getProgress()", 100);
  }
 }
}
 
function getProgress(){
 loop++;
 document.getElementById("showNum2").innerHTML = "用时"+loop/10+"秒";
 sendURL();
}
var interval;
function startProgress(){
	document.getElementById("progressouter").style.display="block";
	document.getElementById("uploadform").style.display="none";
	setTimeout("getProgress()", 600);
}
</script>
<div style="float:left;">
	<div id="progressouter" style= "width: 300px; height: 5px; border: 1px solid #FF0000; display:none;">
		<div id="progressinner" style="position: relative; height: 5px; background-color: #0000FF; width: 0%; "></div>
	</div>
	<div id='showNum' style="float:left;font-size:12px;height:10px;"></div>
	<div id='showNum2' style="float:right;font-size:12px;height:10px;"></div>
</div>
<div id="uploadform">
	<form enctype="multipart/form-data" id="upload_form" action="?do=upload_documents" method="POST">
		<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key"  value="<?php echo $id?>"/>
		<input type="file" id="test_file" name="test_file" style="width:245px;"/>
		<input onclick="startProgress(); return true;" type="submit" value="上传"/>
	</form>
</div>
<?php
}
?>
<?php
function upload_documents(){
	set_time_limit(600);
    $size = 209715200;//设置上传的最大值，B字节
    $uploaddir = "/uploadfiles/document/";//设置文件保存目录 注意包含/    
    //$patch="http://localhost/uploadfiles/image/";//程序所在路径
//    $type = array("doc","docx","ppt","swf","pdf","txt");//设置允许上传文件的类型 
    $a=strtolower(fileext($_FILES['test_file']['name']));
	if($_SERVER['REQUEST_METHOD']=='POST') {
	   //判断文件类型
//	   if(!in_array(strtolower(fileext($_FILES['test_file']['name'])),$type)){
//			$text=implode(",",$type);
//			exit("<span style=' height:50px;line-height:25px;font-size:13px;background-color:#EDEDED;color:#FF0000;'>上传失败！您只能上传以下类型文件:<br /> ".$text."&nbsp;&nbsp;&nbsp;&nbsp;<a href='?'>重新上传!</a></span>");
//	   }
//	   //生成目标文件的文件名    
//	   else{
		   if($_FILES["test_file"]["size"] > $size){
	?>
	<div style="height:20px; line-height:20px; background-color:#EDEDED;font-size:13px;margin:5px 0px;">上传失败，大小超出了限制！<a href="?">重新上传!</a></div>
	<?php
			   exit;
		   }
		   $filename=explode(".",$_FILES['test_file']['name']);
		   do
		   {
			   $filename[0]=date("ymdHis").rand(1000,9999); //设置随机数
			   $name=implode(".",$filename);
			   //$name1=$name.".Mcncc";
			   $uploadfile=$uploaddir.$name;
		   }
			while(file_exists($uploadfile));
			if(is_uploaded_file($_FILES['test_file']['tmp_name'])){ //判断是否合法上传
				if (move_uploaded_file($_FILES["test_file"]["tmp_name"],dirname($_SERVER['SCRIPT_FILENAME']).$uploadfile)){
	?>
	<script type="text/javascript">
    parent.addform.address.value='<?php echo $uploadfile; ?>';parent.addform.size.value='<?php if(floor($_FILES["test_file"]["size"]*100/1024/1024)/100 == 0){echo floor($_FILES["test_file"]["size"]*1000/1024/1024)/1000;}else{echo floor($_FILES["test_file"]["size"]*100/1024/1024)/100;} ?>';
</script>
	<div style="height:20px; line-height:20px; background-color:#EDEDED;font-size:13px;margin:5px 0px;">上传成功</div>
	<?php
				}
				else{
	?>
	<div style="height:20px; line-height:20px; background-color:#EDEDED;font-size:13px;margin:5px 0px;">上传失败！<a href="?">重新上传!</a></div>
	<?php
				}
			}
//	   }
	}
}
function fileext($filename)
{
    return substr(strrchr($filename, '.'), 1);
}
?>
</body>
</html>